package com.cloudsigns.factorymanager2.produce.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartScheduleBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class ProductOrderEditModel implements IProductOrderEditModel {

    @Override
    public void getProductOrderEdit(final long beginDate, final long finishDate, final String orderId, final String productId, final List<WorkPartBean> workPartList, final OnProductOrderEditListener onProductOrderEditListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.PRODUCT_ORDER_EDIT);
                HttpResponse response;
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("beginDate", beginDate);
                    jsonObject.put("finishDate", finishDate);
                    jsonObject.put("orderId", orderId);
                    jsonObject.put("productId", productId);

                    if (null != workPartList && workPartList.size() > 0) {
                        JSONArray workPartListJsonArr = new JSONArray();

                        for (int i = 0; i < workPartList.size(); i++) {
                            WorkPartBean workPartBean = workPartList.get(i);
                            JSONObject workPartBeanObj = new JSONObject();
                            workPartBeanObj.put("amount", workPartBean.getAmount());
                            workPartBeanObj.put("completeDate", workPartBean.getCompleteDate());
                            workPartBeanObj.put("lastWorkPart", workPartBean.isLastWorkPart());
                            workPartBeanObj.put("workPartName", workPartBean.getWorkPartName());
                            workPartBeanObj.put("shapeMap", workPartBean.getShapeMap());

                            JSONObject shapeTypeObj = new JSONObject();
                            shapeTypeObj.put("border", workPartBean.getShapeType().getBorder());
                            shapeTypeObj.put("borderBottom", workPartBean.getShapeType().getBorderBottom());
                            shapeTypeObj.put("borderLeft", workPartBean.getShapeType().getBorderLeft());
                            shapeTypeObj.put("borderRight", workPartBean.getShapeType().getBorderRight());
                            shapeTypeObj.put("borderTop", workPartBean.getShapeType().getBorderTop());
                            shapeTypeObj.put("desc", workPartBean.getShapeType().getDesc());
                            shapeTypeObj.put("diameter", workPartBean.getShapeType().getDiameter());
                            shapeTypeObj.put("height", workPartBean.getShapeType().getHeight());
                            shapeTypeObj.put("thick", workPartBean.getShapeType().getThick());
                            if (null != workPartBean.getShapeType().getType()) {
                                CommonEnum.ProductShapeType type = workPartBean.getShapeType().getType();
                                if (type == CommonEnum.ProductShapeType.RECTANGLE) {
                                    shapeTypeObj.put("type", "RECTANGLE");
                                } else if (type == CommonEnum.ProductShapeType.ROUND) {
                                    shapeTypeObj.put("type", "ROUND");
                                } else if (type == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                                    shapeTypeObj.put("type", "SPECIALSHAPE");
                                }
                            }
                            shapeTypeObj.put("width", workPartBean.getShapeType().getWidth());
                            workPartBeanObj.put("shapeType", shapeTypeObj);
                            workPartBeanObj.put("startDate", workPartBean.getStartDate());
                            workPartBeanObj.put("unit", workPartBean.getUnit());

                            if (null != workPartBean.getWorkPartScheduleList() && workPartBean.getWorkPartScheduleList().size() > 0) {
                                JSONArray workPartScheduleJsonArr = new JSONArray();

                                for (int j = 0; j < workPartBean.getWorkPartScheduleList().size(); j++) {
                                    WorkPartScheduleBean workPartScheduleBean = workPartBean.getWorkPartScheduleList().get(j);
                                    JSONObject workPartScheduleJsonObj = new JSONObject();
                                    workPartScheduleJsonObj.put("amount", workPartScheduleBean.getAmount());
                                    workPartScheduleJsonObj.put("scheduleDate", workPartScheduleBean.getScheduleDate());

                                    workPartScheduleJsonArr.put(workPartScheduleJsonObj);
                                }
                                workPartBeanObj.put("workPartScheduleItemRequestList", workPartScheduleJsonArr);
                            } else {
                                workPartBeanObj.put("workPartScheduleItemRequestList", new JSONArray());
                            }

                            workPartListJsonArr.put(workPartBeanObj);
                        }
                        jsonObject.put("workPartRequestList", workPartListJsonArr);
                    } else {
                        jsonObject.put("workPartRequestList", new JSONArray());
                    }
                    Log.e("ProductOrderEditModel", "jsonObject=" + jsonObject.toString());
                    Log.e("ProductOrderEditModel", "ProjectUtil=" + ProjectUtil.PRODUCT_ORDER_EDIT);

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.setEntity(se);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    Log.e("ProductOrderEditModel", "result=" + result);
                    if (null != onProductOrderEditListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            onProductOrderEditListener.successed();
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            onProductOrderEditListener.failed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnProductOrderEditListener {
        void successed();

        void failed(String desc);
    }
}
